<?php

namespace App\Http\Middleware;

use App\Models\WxUser;
use Closure;
use Illuminate\Support\Facades\Session;

class DeclaimerCheckWXLogin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (!Session::has('wx_user')) {
            return redirect('/declaimer/users/oauth');
        }

        $sessionUser = Session::get('wx_user');
        if (!WxUser::find($sessionUser->id)) {
            Session::forget('wx_user');
            return redirect('/declaimer/users/oauth');
        }

        return $next($request);
    }
}
